package de.carcophan.jimdb.gwt.client.view;

import java.util.Arrays;
import java.util.List;

import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;

import de.carcophan.jimdb.gwt.client.presenter.MovieGridPresenterImpl.Display;
import de.carcophan.jimdb.gwt.shared.entites.UserMovies;

public class MovieGridViewImpl extends Composite implements Display {

   private static MovieGridViewImplUiBinder uiBinder = GWT.create(MovieGridViewImplUiBinder.class);

   interface MovieGridViewImplUiBinder extends UiBinder<Widget, MovieGridViewImpl> {
   }

   // @UiField
   // SimplePager pager;
   @UiField
   CellTable<UserMovies> table;
   private ListDataProvider<UserMovies> dataProvider;

   public MovieGridViewImpl() {
      initWidget(uiBinder.createAndBindUi(this));
      dataProvider = new ListDataProvider<UserMovies>();
      dataProvider.addDataDisplay(table);

      TextColumn<UserMovies> column = new TextColumn<UserMovies>() {
         @Override
         public String getValue(UserMovies um) {
            return "";
         }

         @Override
         public void render(Context context, UserMovies um, SafeHtmlBuilder sb) {
            sb.appendHtmlConstant("<a href='" + um.getMovie().getImdbUrl() + "' target='_blank'>"
                  + um.getMovie().getImdbId() + "</a>");
            super.render(context, um, sb);
         }
      };
      table.addColumn(column, "Imdb-Id");

      column = new TextColumn<UserMovies>() {
         @Override
         public String getValue(UserMovies um) {
            return um.getMovie().getTitle();
         }
      };
      table.addColumn(column, "Title");

      column = new TextColumn<UserMovies>() {
         @Override
         public String getValue(UserMovies um) {
            return String.valueOf(um.getMovie().getYear());
         }
      };
      table.addColumn(column, "Year");

      column = new TextColumn<UserMovies>() {
         @Override
         public String getValue(UserMovies um) {
            return "";
         }

         @Override
         public void render(Context context, UserMovies um, SafeHtmlBuilder sb) {
            String hexColor = um.getTag().getHexColor();
            String tag = um.getTag().getName();
            String fontColor = "<strong><font color='#" + hexColor + "'>";
            String tagDisplay = fontColor + tag + "</font></strong>";
            sb.appendHtmlConstant(tagDisplay);
            super.render(context, um, sb);
         }
      };
      table.addColumn(column, "Tag");

      column = new TextColumn<UserMovies>() {
         @Override
         public String getValue(UserMovies um) {
            return String.valueOf(um.getMovie().getRating());
         }
      };
      table.addColumn(column, "Rating");

      column = new TextColumn<UserMovies>() {
         @Override
         public String getValue(UserMovies um) {
            return um.getMovie().getRuntime();
         }
      };
      table.addColumn(column, "Runtime");

      column = new TextColumn<UserMovies>() {
         @Override
         public String getValue(UserMovies um) {
            String actors = um.getMovie().getActors();
            if (actors != null) {
               String[] split = actors.split(",");
               System.out.println(Arrays.toString(split));
               if (split.length > 3) {
                  actors = split[0] + ", " + split[1] + ", " + split[2];
               }
            }

            return actors;
         }
      };
      table.addColumn(column, "Actors");

      // final SingleSelectionModel<UserMovies> selectionModel = new
      // SingleSelectionModel<UserMovies>();
      // table.setSelectionModel(selectionModel);
      // selectionModel.addSelectionChangeHandler(new
      // SelectionChangeEvent.Handler() {
      // public void onSelectionChange(SelectionChangeEvent event) {
      // UserMovies selected = selectionModel.getSelectedObject();
      // if (selected != null) {
      // Window.alert("You selected: " + selected.getMovie().getTitle());
      // }
      // }
      // });
   }

   public MovieGridViewImpl(String firstName) {
      initWidget(uiBinder.createAndBindUi(this));

   }

   @Override
   public void setMovies(List<UserMovies> userMovies) {
      dataProvider.setList(userMovies);
      // table.setRowData(userMovies);
   }

}
